//---------------------------------------------------------------------
//  This file is part of the Background Motion solution.
// 
//  Copyright (C) Mindscape (TM).  All rights reserved.
//  http://www.mindscape.co.nz
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System.Collections;

namespace Mindscape.Web.UI.WebControls.Binding
{
  /// <summary>
  /// Collection of individual DataBindingItems. Implemented explicitly as
  /// a CollectionBase class rather than using List#WebDataBindingItems#
  /// so that Add can be overridden
  /// </summary>
  public class WebDataBindingItemCollection : CollectionBase
  {
    /// <summary>
    /// Internal reference to the WebDataBinder object
    /// that is passed to the individual items if available
    /// </summary>
    private WebDataBinder _binder = null;

    /// <summary>
    /// Preferred Constructor - Add a reference to the WebDataBinder object here
    /// so a reference can be passed to the children.
    /// </summary>
    public WebDataBindingItemCollection(WebDataBinder binder)
    {
      _binder = binder;
    }

    /// <summary>
    /// Not the preferred constructor - If possible pass a reference to the
    /// Binder object in the overloaded version.
    /// </summary>
    public WebDataBindingItemCollection()
    {
    }

    /// <summary>
    /// Public indexer for the Items
    /// </summary>
    public WebDataBindingItem this[int index]
    {
      get { return InnerList[index] as WebDataBindingItem; }
      set { InnerList[index] = value; }
    }

    /// <summary>
    /// Add a WebDataBindingItem to the collection
    /// </summary>
    public void Add(WebDataBindingItem item)
    {
      if (_binder != null)
      {
        item.Page = _binder.Page;
        item.Binder = _binder;

        if (_binder.DesignMode)
        {
          UpdateListInDesignMode();
        }
      }

      InnerList.Add(item);
    }

    /// <summary>
    /// Add a WebDataBindingItem to the collection
    /// </summary>
    public void AddAt(int index, WebDataBindingItem item)
    {
      if (_binder != null)
      {
        item.Page = _binder.Page;
        item.Binder = _binder;

        if (_binder.DesignMode)
        {
          UpdateListInDesignMode();
        }
      }

      InnerList.Insert(index, item);
    }

    /// <summary>
    /// We have to delete 'empty' items because the designer requires items to be 
    /// added to the collection just for editing. This way we may have one 'extra'
    /// item, but not a whole long list of items.
    /// </summary>
    private void UpdateListInDesignMode()
    {
      if (_binder == null)
      {
        return;
      }

      bool Update = false;

      for (int x = 0; x < Count; x++)
      {
        if (string.IsNullOrEmpty(this[x].BindingSource) && string.IsNullOrEmpty(this[x].BindingSourceMember))
        {
          RemoveAt(x);
          Update = true;
        }
      }

      if (Update)
      {
        _binder.NotifyDesigner();
      }
    }
  }
}